%% Epistimonikos Ypologismos
%Ergasia 4_Meros 1o
%Xenofwntos Antwnia
%Aem:750



%%A) ORTHWGWNIOI PINAKES
%Askhsh 1:

clear;
clc;

disp('OR8OGWNIOI PINAKES');
disp('Askhsh 1');
%orthogwnioi einai oi pinakes pou to eswteriko tous dianisma einai 0,opote kai ta duo pio katw dianusmata einai orthogwnia otan to x=0 to dianusma b einai monadiko.
a=[2 1 1]'
b=[-1 1 1]

x=b*a


pause
clc;


%Askhsh 2:

disp('Askhsh 2');
%dianysmata ta opoia einai aneksarthta metaksi tous kai to eswteriko tous ginomeno einai mhden

a=[2 1 1]';
b=[1 2 -4]';
c=cross(a,b);
figure(1);clf;
drawVector([a b c],{'a','b','c'});
view(-160,7)

pause


%Askhsh 3:

disp('Askhsh 3');
%%Oi sthles tou pinaka einai oi aktines tis elleipsis


A = [a b c]; 
X = sampleUnitSphere(3,200);
AX = A*X; 
figure(2);
clf; hold on; 
plot3( X(1,:), X(2,:), X(3,:),'b.');
view(-60,10) 
plot3(AX(1,:),AX(2,:),AX(3,:),'g.'); 
drawVector(A)
hold off; 
axis square equal; 
grid on;
norma=norm(A)
pause


%Askhsh 4:

disp('Askhsh 4');
%Oi sthles tou pinaka exoun tin shmasia twn aktinwn ths elipshs.


B = [a b c];
C = diag([1/norm(a) 1/norm(b) 1/norm(c)]);
A = B*C;
ABX=A*B*X;
AABX=A*A*B*X;
AAABX=A*A*A*B*X;
figure(3);
clf; hold on; 
plot3( X(1,:), X(2,:), X(3,:),'b.');
view(-60,10) 
plot3(ABX(1,:),ABX(2,:),ABX(3,:),'g.'); 
plot3(AABX(1,:),AABX(2,:),AABX(3,:),'g.'); 
plot3(AAABX(1,:),AAABX(2,:),AAABX(3,:),'g.'); 
hold off; 
axis square equal; 
grid on;


pause


%Askhsh 5:

disp('Askhsh 5');
%Antistrofos tou kanonikopoihmenou pinaka A invA2=inv(Q)

clear all;
A=[1 -1;1 1];
e1 = [1;0];
e2 = [0;1];
figure(4);
clf; hold on; 
drawVector(A, {'a','b'});
drawVector([e1 e2], 'gs', {'e_1', 'e_2'})
drawVector(inv(A), {'inv(a)','inv(b)'});


a=[1 -1]';
b=[1 1]';

q1=a/(norm(a'));
b2=b-(q1'*b)*q1
q2=b2/(norm(b2));
Q=[q1 q2]
drawVector([q1 q2], 'r*', {'q1', 'q2'})
hold off;

invA2=inv(Q)

clear all;
pause




%%B)NORMES PINAKWN
% Askhsh 1:

disp('NORMES PINAKWN');
disp('Askhsh 1');


B=[1 3;1 1];

C=reshape(1:4,2,2);

X=sampleUnitSphere(2, 100);
BX=B*X;
CX=C*X;

figure(1);clf;hold on;
plot(X(1,:),X(2,:),'b.');
plot(BX(1,:),BX(2,:),'r.');
plot(CX(1,:),CX(2,:),'g')
title('Sxesh a kai b')
hold off
axis square equal

pause


%Askshsh 2:

disp('Askhsh 2');
%o pinakas -B tha prepei grafika na exei antitheth fora apo ton B kai o B' na exei opou x ta y tou B kai opou  y ta x tou B

B1=-B;
B2=B';
B1X=B1*X;
B2X=B2*X;

figure(2);clf;hold on;
plot(X(1,:),X(2,:),'b.');
plot(B1X(1,:),BX(2,:),'r.');
plot(B2X(1,:),B2X(2,:),'g')
hold off
axis square equal

pause


%Askhsh 3:

disp('Askhsh 3');


A=[-2 3;
   -3 1];

B=[1 3;1 1];

AX=A*X;
BX=B*X;
for i=1:6
    AX=A*AX;
    BX=B*BX;
    figure(i);clf;hold on;
plot(X(1,:),X(2,:),'b.');
plot(AX(1,:),AX(2,:),'r.');
plot(BX(1,:),BX(2,:),'g.');
hold off
axis square equal
end;

pause


%Askhsh 4:

disp('Askhsh 4');
%%Mporoume na doume tis takseis twn pinakwn 
%Taksi A=3
%Taksi B=2

A=[1 3 -2;8 0 1;2 2 6]
B=reshape(1:9,3,3);

figure(1);hold on;clf;
drawVector(A,{'a_1','a_2','a_3'});
drawVector(B,{'b_1','b_2','b_3'});
view(60,10)
hold off;

pause


%Askshss 5:

disp('Askhsh 5');
%To monadiaio dianysma me thn megaluterh norma tha einai to [1 1 1 ]
%Einai pio dyskolo na broume ena elaxisto mege8os gia ta barh tou A

clear all;

A=[1 3 -2;8 0 1;2 2 6];
x=[1 1 1];

xA=norm(x)*A
Ax=A*x';
norm(Ax);

pause


%Askhshs 6:

disp('Askhsh 6');

A=[1 3 -2;8 0 1;2 2 6]
B=reshape(1:9,3,3);

AB=A*B;
x=norm(AB)

normA=norm(A);
normB=norm(B);
normAB=normA*normB

clear all;



%%C)PROVOLES
%Askhsh 1:

disp('PROBOLES');
disp('Askhsh 1');

%Gia ato pinaka A
A=[1 0;0 1;1 0];
B=[1 2;0 1;1 0];

a=[1 2 3]';

figure(1);clf;hold on;
drawVector(a,'r.',{'a'});
P_orth=A*A';
a_orth=P_orth*a;
drawVector([a_orth],'b.',{'a_orth'})
drawSpan(A);
hold off;
view(150,15);

%Gia to pinaka B
figure(2);clf;hold on;
drawVector(a,'r.',{'a'});
P_orth=B*B';
a_orth=P_orth*a;
drawVector([a_orth],'b.',{'a_orth'})
drawSpan(B);
hold off;
view(150,15);

pause

clear all;


%Askhsh 2:

disp('Askhsh 2');


%Gia to pinaka A
A = [1 0; 0 1; 1 0];
a = [1 2 3]'
n = [-.5 6 3]';
figure(1);clf;hold on;
drawVector(a, 'r.', {'a'}); 
P_oblique = A*[1 0 0;0 1 0]*inv([A n]);
drawVector(n, 'g.')
a_oblique = P_oblique*a;
drawVector([a_oblique], 'b.', {'a_{oblique}'})
drawSpan(A) 
hold off 
view(150, 15)

%Gia to  pinaka B 
B = [1 2; 0 1; 1 0];
a = [1 2 3]'
n = [-.5 6 3]';
figure(2);clf;hold on;
drawVector(a, 'r.', {'a'});  
P_oblique = B*[1 0 0;0 1 0]*inv([B n]);
drawVector(n, 'g.')
a_oblique = P_oblique*a;
drawVector([a_oblique], 'b.', {'a_{oblique}'})
drawSpan(B) 
hold off 
view(150, 15)


clear all;



%%D)ANTISTROFOS PINAKAS
%Askhsh 1:

disp('Antistrofos Pinaka');

%Nai mporoume na mantepsoume ta stoixeia ths prwths sthlhs tou antistrofou pinaka gewmetrika 
%Mporoume na manteyoume kai gia th deuterh  sthlh
%Ta proshma mporoume na ta vroume  afou  gnwrizoume pws mporoume na vroume ton antistrofo  enos 2*2 pinaka 


%Askhsh 2:

disp('Askhsh 2');

A=[1 1;-2 2];
e1 = [1;0]; 
e2 = [0;1];
K=[0;0];
figure(1); clf; hold on;
drawVector(A, {'a','b'});
drawVector([e1 e2], 'gs', {'e_1', 'e_2'})
hold off;

invA = inv(A)
P1 = A*diag(invA(:,1));  
P2 = A*diag(invA(:,2));
figure(1); hold on;
drawVector(P1, 'rd') 
drawVector(P2, 'ms')
drawLine([e1 P1(:,1)]);
drawLine([e1 P1(:,2)]); 
drawLine([e2 P2(:,1)]);
drawLine([e2 P2(:,2)]);
drawLine([P2(:,1) P1(:,1)], '2m-');
drawLine([P2(:,2) P1(:,2)], '2r-');
drawLine([K P1(:,1)], '2r-');
drawLine([K P2(:,2)], '2m-');
drawLine([K e1], '2g-');
drawLine([K e2], '2g-');
hold off

pause


clear all;



%%E)PARAGONTOPOIHSH LU
%Askhsh 1:

disp('Paragontopoihsh LU');
disp('Askhsh 1');

clear all;

A = [ 2 4; 3 6 ];
b = [4 7]'; 
x = A\b;
 figure(1); clf; hold on; 
 drawPlane(A(1,:), b(1), 'b'); 
 drawPlane(A(2,:), b(2), 'g');
 hold off 
title('Ax=b in the row space', 'FontSize', 14);
 figure(3); hold on; 
 plot(x(1), x(2), 'rx', 'MarkerSize',16, 'LineWidth',2)
 hold off;
 
pause


% Askhsh 2:

disp('Askhsh 2');
 
A = [3 12 1; 
     12 0 2;
    0 2 3];
b = [2.36 5.26 2.77]';
x = A\b;
Pb = A*diag(x);
figure(1);clf;hold on; 
drawVector(A, {'a_1','a_2','a_3'}); 
drawVector(b, 'rs', {'b'}); 
drawLine([b Pb(:,1)]); 
hold off;
title('Ax=b in the column space', 'FontSize', 14);
figure(3); clf; hold on;
drawPlane(A(1,:), b(1), 'b'); 
drawPlane(A(2,:), b(2), 'g'); 
drawPlane(A(3,:), b(3), 'r'); 
plot3(x(1),x(2),x(3), 'mx', 'LineWidth', 4, 'MarkerSize', 15)
hold off 
title('Ax=b in the row space', 'FontSize', 14);
 
fprintf('To mhlo kostizei %12f  ,  i mpanana kostizei %12f kai o ananas kostizei %12f \n',x(1),x(2),x(3))
 
pause
 
clear all; 



%Askhsh 3:

disp('Askhsh 3');

%In the column space
U = [ 4 1 ; 0 3.5 ]; 
b = [2 -3]';
figure(3); clf;
hold on;
drawPlane(A(1,:), b(1), 'b'); 
drawPlane(A(2,:), b(2), 'g');
hold off
title('Ax=b in the row space', 'FontSize', 14);
figure(3);
hold on;
plot(x(1), x(2), 'rx', 'MarkerSize', 16, 'LineWidth', 2);
hold off;

%%In the row space
A = [ 4 1 ; 0 3.5 ];
b = [2 -3]'; 
x = A\b;
figure(2); clf;hold on;
drawVector(A, {'a_1', 'a_2'}) 
drawVector(b, 'rs', {'b'});
Pb = A*diag(x); 
drawVector(Pb, 'g+', {'x_{11}\cdot a_1', 'x_{21}\cdot a_2'}); 
drawLine([b Pb(:,1)]);
drawLine([b Pb(:,2)]);hold off;
title('Ax=b in the column space', 'FontSize', 14);

pause;
 
clear all;
 
 
%%Askhsh 4:
 
disp('Askhsh 4');

 U = [ 2 1 1 0; 0 1 1 1; 0 0 2 2; 0 0 0 2 ]
 I=eye(4); 
 for j=1:4 
     b=I(1:4,j); 
     x(1:4,j)=backSub(U,b); 
 end
 V=U*x; 
 disp('O pinakas x einai o antistrofos tou U');
 display(x); 
 display(V);
 
 pause
 
 clear all;

 
 
%Askshsh 5:

 disp('Askhsh 5');

 
 A = [ 2 1 1 0;
     4 3 3 1; 
     8 7 9 5;
     6 7 9 8 ]; 
 [L, U] = slu(A,1);
 
 pause

 
 
 %Askhsh 6:
 
 disp('Askhsh 6');
 
 clear all;
 
 A = [ 2 1 1 0;
     4 3 3 1; 
     8 7 9 5;
     6 7 9 8 ] 
 
 b=(2:2:8)'
 
 [L U]=slu(A,1)
 x=forSub(A,b)
 
 pause
 
 clear all;

 

%Askhsh 7:
 
disp('Askhsh 7');
%diaferoun oi duo pinakes
%aferontas apo thn lu to shmeio tou kwdika opou kanei enallages grammwn. 

clear all;

A = [ 2 1 1 0;
    4 3 3 1; 
    8 7 9 5;
    6 7 9 8 ]

[L U]=lu(A)

[L2 U2]=slu(A,1)






